package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先创建一个购物车对象
        ShoppingCart shoppingCart = new ShoppingCart(); //里面都是空的
        //需要给属性赋值 通过对象拷贝  源和目标
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置用户id
        Long userId =  BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断商品是否存在购物车  需要根据条件查询购物车 返回一个列表 只会有一个 后面还有查看 预见性 方便
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //存在就数量加一 如果list 不为空 且
        if(list!=null && list.size()>0){ // 两种情况要不就没有 要不就一条 因为只有一个用户 加菜
            //获取到了  第一个也是唯一的一条数据
            ShoppingCart cart = list.get(0);
            //数量加一就 可
            cart.setNumber(cart.getNumber()+1);//需要执行一条修改语句
            shoppingCartMapper.updateNumber(cart);
        }else {
            //如果没有添加过购物车的话就插入 一条购物车数据
            //判断 本次添加 购物的菜品或 套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId!=null){
                //添加到购物车 的是菜品
                //查看实体类参数  需要的参数
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //添加到 购物车 的是套
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }
    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {

        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void cleanShoppingCart() {
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.clean(cart);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);

        //设置用户id
        cart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> cartList = shoppingCartMapper.list(cart);

        if (cartList!=null&&cartList.size()>0){
            ShoppingCart cart1 = cartList.get(0);
            //获得数量
            Integer number = cart1.getNumber();
            if (number>1){
                cart1.setNumber(cart1.getNumber()-1);//需要执行一条修改语句
                shoppingCartMapper.updateNumber(cart1);
            }else {
                //有两个值
                // user setmeal id
                shoppingCartMapper.clean(cart1);
            }
        }

    }


}
